<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dictionary_model extends Model {

    public $table = "";
    public $id = 0;
    public $name = '';
    public $email = '';
    public $phone = '';
    public $share = '';
    public $message = 0;
    public $dt = 0;
    public $ip = 0;

    /**
     * Constructor
     * @access public
     * @return void
     */
    function __construct() {
        parent::__construct();
        $this->table = DB_PREF . 'dictionary';
    }

    /**
     * Get a single entry by its ID
     * @param integer $id
     * @return obejct Dictionary_model
     */
    public function get($id) {
        $result = $this->db->select()->from($this->table)->where('id', $id)->get()->row();
        $obj = FALSE;
        if ($result) {
            $obj = new Dictionary_model();
            $obj->id = $result->id;
            $obj->name = $result->name;
            $obj->email = $result->email;
            $obj->phone = $result->phone;
            $obj->share = $result->share;
            $obj->message = $result->message;
            $obj->dt = $result->dt;
            $obj->ip = $result->ip;
        }
        return $obj;
    }

    public function getAll($filter = array()) {
        if (isset($filter ['field']) and strlen($filter ['field'])) {
            $this->db->select($filter ['field']);
        } else {
            $this->db->select();
        }
        $this->db->from($this->table);
        if (isset($filter['email']) AND strlen($filter['email'])) {
            $this->db->like('email', $filter['email']);
        }
        if (isset($filter['not_share']) AND strlen($filter['not_share'])) {
            $this->db->where('share !=', $filter['not_share']);
        }
        if (isset($filter['name']) AND strlen($filter['name'])) {
            $this->db->like('name', $filter['name']);
        }
        if (isset($filter['dt_g']) AND strlen($filter['dt_g'])) {
            $this->db->where('dt >=', $filter['dt_g']);
        }
        if (isset($filter['dt_l']) AND strlen($filter['dt_l'])) {
            $this->db->where('dt <=', $filter['dt_l']);
        }
        if (isset($filter['dt']) AND strlen($filter['dt'])) {
            $this->db->where('dt', $filter['dt']);
        }
        $order = 'dt';
        $this->db->order_by($order);
        $query = $this->db->get();
        return $query;
    }

    /**
     * Get entries by filter array
     * @param mixed $filter
     * @return mixed
     */
    public function getByParams($limit, $dir = 'ASC', $sort = 'title', $start, $filter = array()) {
        $this->db->select()->order_by($sort, $dir);
        if (isset($filter['not_share']) AND strlen($filter['not_share'])) {
            $this->db->where('share !=', $filter['not_share']);
        }
        if (isset($filter['email']) AND strlen($filter['email'])) {
            $this->db->like('email', $filter['email']);
        }
        if (isset($filter['name']) AND strlen($filter['name'])) {
            $this->db->like('name', $filter['name']);
        }
        if (isset($filter['dt_g']) AND strlen($filter['dt_g'])) {
            $this->db->where('dt >=', $filter['dt_g']);
        }
        if (isset($filter['dt_l']) AND strlen($filter['dt_l'])) {
            $this->db->where('dt <=', $filter['dt_l']);
        }
        if (isset($filter['dt']) AND strlen($filter['dt'])) {
            $this->db->where('dt', $filter['dt']);
        }
        $query = $this->db->get($this->table, $limit, $start);
        return $query;
    }

    /**
     * Add a new entry
     * @return integer
     */
    public function insert() {
        $data = array(
            'name' => $this->name,
            'email' => $this->email,
            'phone' => $this->phone,
            'share' => $this->share,
            'message' => $this->message,
            'dt' => $this->dt,
            'ip' => $this->ip
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    /**
     * Update an existing entry
     * @return void
     */
    public function update() {
        $data = array(
            'name' => $this->name,
            'email' => $this->email,
            'phone' => $this->phone,
            'share' => $this->share,
            'message' => $this->message,
            'dt' => $this->dt,
            'ip' => $this->ip
        );
        $this->db->where('id', $this->id);
        $this->db->update($this->table, $data);
        return $this->id;
    }

    public function delete($filter = array()) {
        if (!count($filter)) {
            $this->db->where('id', $this->id);
        } else {

        }
        $this->db->delete($this->table);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

}